//
//  MCMMCViewController.m
//  TLYL
//
//  Created by MC on 2017/7/6.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "MCMMCViewController.h"
#import "UIImage+Extension.h"

@interface MCMMCViewController ()
{
@private
    ZCSlotMachine *_slotMachine;
    UIButton *_startButton;
    UIView *_slotContainerView;
    NSArray *_slotIcons;
}
@property (nonatomic,strong)NSTimer * timer;

@end


@implementation MCMMCViewController


- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self  updatePreferredContentSizeWithTraitCollection:self.traitCollection];
    [self setProperty];
    [self  createUI];
    //    [self  start];
    
}
- (UIImage*)GetImgWithText:(NSString*)text{
    
    UIImage * image=[UIImage imageNamed:@"全选中"];
    
    
    //    UILabel * lab=[[UILabel alloc]init];
    //    lab.frame=CGRectMake(0,0, image.size.width, image.size.height);
    ////    [image add];
    
    
    UIFont*font = [UIFont systemFontOfSize:10];
    
    NSDictionary*dict =@{NSFontAttributeName:font,NSForegroundColorAttributeName:[UIColor whiteColor]};
    
    CGSize textSize = [text sizeWithAttributes:dict];
    
    //绘制上下文
    
    UIGraphicsBeginImageContext(image.size);
    
    [image drawInRect:CGRectMake(0,0, image.size.width, image.size.height)];
    
    int border =10;
    
    CGRect re = {CGPointMake(image.size.width- textSize.width- border, image.size.height- textSize.height- border), textSize};
    
    //此方法必须写在上下文才生效
    
    [text drawInRect:re withAttributes:dict];
    
    UIImage*newImage =UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return newImage;
    
}

-(NSArray *)createArr{
    NSMutableArray * marr=[[NSMutableArray alloc]init];
    for (int i=0; i<10; i++) {
        UIImage * img=[self GetImgWithText:[NSString stringWithFormat:@"%d",i]];
        [marr addObject:img];
    }
    return marr;
}

-(void)setProperty{
    self.view.backgroundColor=[UIColor  whiteColor];
    _slotIcons=[self createArr];
    //    _slotIcons = [NSArray arrayWithObjects:
    //                  [UIImage drawTextToImage:@"0" addToView:[UIImage imageNamed:@"goucai_number_selected"]], [UIImage drawTextToImage:@"1" addToView:[UIImage imageNamed:@"goucai_number_selected"]],[UIImage drawTextToImage:@"2" addToView:[UIImage imageNamed:@"goucai_number_selected"]],[UIImage drawTextToImage:@"3" addToView:[UIImage imageNamed:@"goucai_number_selected"]],[UIImage drawTextToImage:@"4" addToView:[UIImage imageNamed:@"goucai_number_selected"]],[UIImage drawTextToImage:@"5" addToView:[UIImage imageNamed:@"goucai_number_selected"]],[UIImage drawTextToImage:@"6" addToView:[UIImage imageNamed:@"goucai_number_selected"]],[UIImage drawTextToImage:@"7" addToView:[UIImage imageNamed:@"goucai_number_selected"]],[UIImage drawTextToImage:@"8" addToView:[UIImage imageNamed:@"goucai_number_selected"]],[UIImage drawTextToImage:@"9" addToView:[UIImage imageNamed:@"goucai_number_selected"]],nil];
}


-(void)createUI{
    
    _slotMachine = [[ZCSlotMachine alloc] initWithFrame:CGRectMake(0, 10, 300, 200)];
    _slotMachine.backgroundColor = [UIColor whiteColor];
    _slotMachine.delegate = self;
    _slotMachine.dataSource = self;
    [self.view addSubview:_slotMachine];
    __weak typeof(self) weakSelf = self;
    //    [NSTimer scheduledTimerWithTimeInterval:2.0 target:weakSelf selector:@selector(start) userInfo:nil repeats:NO];
    
    [self performSelector:@selector(start) withObject:nil/*可传任意类型参数*/ afterDelay:0.1];
    
    
    
    //    int padding = (275-28*5-10*4)/2.0;
    //    int width_ball=28;
    //    for(int i=0;i<5;i++){
    //        UIButton * btn=[[UIButton alloc]init];
    //        [btn setBackgroundImage:[UIImage imageNamed:@"goucai_number_selected"] forState:UIControlStateNormal];
    //        [self.view addSubview:btn];
    //        [btn setTitle:[NSString stringWithFormat:@"%d",i] forState:UIControlStateNormal];
    //        [btn.titleLabel setTextColor:[UIColor whiteColor]];
    //        btn.frame=CGRectMake(padding+i*(width_ball+10), 20, width_ball, width_ball);
    //        btn.backgroundColor=[UIColor clearColor];
    //
    //    }
    //
    UILabel * lab_title=[[UILabel alloc]init];
    lab_title.text=@"第1次开奖";
    lab_title.font=[UIFont systemFontOfSize:13];
    lab_title.textColor=RGB(100, 100, 100);
    [self.view addSubview:lab_title];
    lab_title.textAlignment=NSTextAlignmentCenter;
    lab_title.frame=CGRectMake(0, 75, 300, 20);
    
    
    UIButton * btn_Stop=[[UIButton alloc]init];
    btn_Stop.backgroundColor=RGB(48, 127, 207);
    [btn_Stop setTitle:@"停止开奖" forState:UIControlStateNormal];
    [btn_Stop.titleLabel setTextColor:[UIColor whiteColor]];
    btn_Stop.layer.cornerRadius=5;
    [btn_Stop addTarget:self action:@selector(action_Stop) forControlEvents:UIControlEventTouchUpInside];
    btn_Stop.clipsToBounds=YES;
    [self.view addSubview:btn_Stop];
    btn_Stop.frame=CGRectMake((300-142)/2.0, 100, 142, 30);
}

#pragma mark-添加定时器
-(void)add_timer{
    //    _timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerAction_KaiJiang) userInfo:nil repeats:YES];
    //    NSRunLoop *currentRunLoop = [NSRunLoop currentRunLoop];
    //    [currentRunLoop addTimer:_timer forMode:NSRunLoopCommonModes];
    //
}

-(void)timerAction_KaiJiang{
    
}

#pragma mark-停止开奖
-(void)action_Stop{
    [self dismissViewControllerAnimated:YES completion:NULL];
    
}

- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
    
    [super willTransitionToTraitCollection:newCollection withTransitionCoordinator:coordinator];
    [self updatePreferredContentSizeWithTraitCollection:newCollection];
    
}
#pragma mark - Private Methods

- (void)start {
    
    //    [NSNumber numberWithInteger:1]
    _slotMachine.slotResults = @[@(1),@(2),@(3),@(4),@(5)];
    
    
    
    //    _slotMachine.isRotating=NO;
    [_slotMachine startSliding];
}




#pragma mark - UIResponder

- (BOOL)canBecomeFirstResponder {
    return YES;
}

//- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
//
//    _startButton.highlighted = YES;
//    [_startButton performSelector:@selector(setHighlighted:) withObject:[NSNumber numberWithBool:NO] afterDelay:0.8];
//
//    [self start];
//}

#pragma mark - ZCSlotMachineDelegate
#pragma mark-摇奖将要开始
- (void)slotMachineWillStartSliding:(ZCSlotMachine *)slotMachine {
    _startButton.enabled = NO;
}

- (void)slotMachineDidEndSliding:(ZCSlotMachine *)slotMachine {
    _startButton.enabled = YES;
}

#pragma mark - ZCSlotMachineDataSource

- (NSArray *)imgIcons_ForSlotsInSlotMachine:(ZCSlotMachine *)slotMachine {
    return _slotIcons;
}

- (NSUInteger)numberOfSlotsInSlotMachine:(ZCSlotMachine *)slotMachine {
    return 5;
}

- (CGFloat)slotWidthInSlotMachine:(ZCSlotMachine *)slotMachine {
    return 60.0f;
}

- (CGFloat)slotSpacingInSlotMachine:(ZCSlotMachine *)slotMachine {
    return 5.0f;
}
#pragma mark-更新高度
- (void)updatePreferredContentSizeWithTraitCollection:(UITraitCollection *)traitCollection{
    
    self.preferredContentSize = CGSizeMake(300, 150);
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
